package com.github.baichuan.application.dashboard.config;

import com.auth0.jwt.algorithms.Algorithm;
import com.github.baichuan.application.dashboard.security.Jwt;
import com.github.baichuan.application.dashboard.security.Principal;
import com.github.baichuan.web.security.JwtCodec;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
public class Component {
    @Autowired
    private Environment env;

    @Bean
    public JwtCodec<Principal> jwtCodec(){
        String secret = env.getRequiredProperty("security.jwt.secret");
        return Jwt.builder()
                .ttl(env.getRequiredProperty("security.jwt.ttl", Integer.class))
                .algorithm(Algorithm.HMAC256(secret))
                .build();
    }
}
